package com.bwie.parkdemo.mvvm.viewmodel;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import com.blankj.utilcode.util.TimeUtils;
import com.bwie.lib_core.base.BaseEntity;
import com.bwie.lib_core.mvvm.base.BaseOBViewModel;
import com.bwie.parkdemo.mvvm.model.AddPunchModel;
import com.bwie.parkdemo.mvvm.model.entity.PunchPointEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class AddPuchViewModel extends BaseOBViewModel<AddPunchModel> {

    public ObservableField<List<Object>>datas;
    public PunchOnItemClickListener itemClickListener;
    public int adapterType = 9;
    private int position;

    public AddPuchViewModel(@NonNull Application application) {
        super(application);
        datas = new ObservableField<>();
        itemClickListener = new PunchOnItemClickListener();
    }

    @Override
    protected AddPunchModel createModel() {
        return new AddPunchModel();
    }

    @Override
    protected void initData() {

    }

    public void setDatas(String pPoint) {
        pPoint.replace("\\","");
        List<Object>list = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(pPoint);
            for (int i = 0;i < jsonArray.length();i ++){
                String jsonStr = jsonArray.getJSONObject(i).toString();
                PunchPointEntity entity = new Gson().fromJson(jsonStr,PunchPointEntity.class);
                list.add(entity);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        datas.set(list);
    }

    //发起打卡请求操作
    /**
     * {
     "isPunch": 1,
     "ppatrolId": 47,
     "pointId":
     "punchTime": "2020-03-09T00:49:56.681Z"
     }
     * */
    public void addPunch(String str){
        try {
            JSONObject job = new JSONObject(str);
            long time = System.currentTimeMillis();
            String punchTime = TimeUtils.millis2String(time,new SimpleDateFormat("yyyy-MM-dd"));
            punchTime +="T";
            punchTime = punchTime+TimeUtils.millis2String(time,new SimpleDateFormat("hh:mm:ss"));
            punchTime +="Z";
            job.put("punchTime", punchTime);
            String jsonStr = job.toString();
            RequestBody body = RequestBody.create(MediaType.parse("application/json"),jsonStr);
            m.request(body).observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onNext(BaseEntity baseEntity) {
        if (baseEntity.statuesCode.equals("200")){
            List<Object>list = new ArrayList<>();
            list.addAll(datas.get());
            PunchPointEntity entity = (PunchPointEntity) list.get(position);
            entity.isPunch = true;
            list.set(position,entity);

        }
    }

    public class PunchOnItemClickListener implements BaseQuickAdapter.OnItemClickListener{

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            Log.e("ZXWQ","PunchOnItemClickListener");
            PunchPointEntity entity = (PunchPointEntity) datas.get().get(position);
            Map<String,Object>map = new HashMap<>();
            map.put("isPunch",1);
            map.put("ppatrolId",47);
            map.put("pointId",entity.pointId);
            addPunch(new Gson().toJson(map));
        }
    }

}
